<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      label-width="68px"
    >
      <el-form-item label="机构名称" prop="institutionId">
        <el-select
          v-model="queryParams.institutionId"
          placeholder="请选择机构名称"
          filterable
          clearable
          @change="getStoreList"
        >
          <el-option
            v-for="item in institutionList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门店名称" prop="storeId">
        <el-select
          v-model="queryParams.storeId"
          placeholder="请选择门店名称"
          filterable
          clearable
          @change="getCabinetList"
        >
          <el-option
            v-for="item in storeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="柜机名称" prop="cabinetId">
        <el-select
          v-model="queryParams.cabinetId"
          placeholder="请选择柜机名称"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in cabinetList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['mc:fault:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >
          批量删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="机构名称" align="center" prop="institutionName" />
      <el-table-column label="门店名称" align="center" prop="storeName" />
      <el-table-column label="柜机名称" align="center" prop="cabinetName" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="联系电话" align="center" prop="contactNumber" />
      <el-table-column label="故障图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <Image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="故障描述" align="center" prop="faultDescribe" show-overflow-tooltip />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 0 ? 'danger' : 'success'">
            {{ scope.row.status == "0" ? "未处理" : "已处理" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="反馈" align="center" prop="feedback" show-overflow-tooltip />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['mc:fault:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            处理
          </el-button>
          <el-button
            v-hasPermi="['mc:fault:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改故障申报对话框 -->
    <el-dialog
      title="处理故障"
      :visible.sync="open"
      width="900px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="editForm" :model="editForm" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="机构名称" prop="institutionName">
              {{ editForm.institutionName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="门店名称" prop="storeName">
              {{ editForm.storeName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="柜机名称" prop="cabinetName">
              {{ editForm.cabinetName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
              {{ editForm.contact }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactNumber">
              {{ editForm.contactNumber }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="故障图片" prop="image">
          <Image-preview :src="editForm.image" multiple :width="150" :height="150" />
        </el-form-item>
        <el-form-item label="故障描述" prop="faultDescribe">
          {{ editForm.faultDescribe }}
        </el-form-item>
        <el-form-item label="反馈" prop="feedback">
          <el-input
            v-model="editForm.feedback"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 8 }"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFault, getFault, delFault, updateFault } from "@/api/mc/fault";
import { listInstitution } from "@/api/mc/institution";
import { listStore } from "@/api/mc/store";
import { listCabinet } from "@/api/mc/cabinet";

export default {
  name: "Fault",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障申报表格数据
      dataList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        institutionId: null,
        storeId: null,
        cabinetId: null,
      },
      // 表单参数
      editForm: {
        id: null,
        institutionId: null,
        storeId: null,
        cabinetId: null,
        faultDescribe: null,
        image: null,
        status: null,
        feedback: null,
        type: null,
      },
      institutionList: [], // 机构列表
      storeList: [], // 门店列表
      cabinetList: [], // 柜机列表
    };
  },
  created() {
    this.getInstitutionList();
    this.getList();
  },
  methods: {
    /** 查询故障申报列表 */
    getList() {
      this.loading = true;
      listFault(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 获取机构列表
    getInstitutionList() {
      listInstitution({ pageNum: 1, pageSize: 1000 }).then((res) => {
        this.institutionList = res.rows;
      });
    },
    // 获取门店列表
    getStoreList() {
      this.queryParams.storeId = null;
      this.queryParams.cabinetId = null;
      this.cabinetList = [];
      this.handleQuery();
      listStore({
        pageNum: 1,
        pageSize: 1000,
        institutionId: this.queryParams.institutionId,
      }).then((res) => {
        this.storeList = res.rows;
      });
    },
    // 获取柜机列表
    getCabinetList() {
      this.queryParams.cabinetId = null;
      this.handleQuery();
      listCabinet({ pageNum: 1, pageSize: 1000, storeId: this.queryParams.storeId }).then(
        (res) => {
          this.cabinetList = res.rows;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.editForm = {
        id: null,
        institutionId: null,
        storeId: null,
        cabinetId: null,
        faultDescribe: null,
        image: null,
        status: null,
        feedback: null,
        type: null,
      };
      this.resetForm("editForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.storeList = [];
      this.cabinetList = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getFault(row.id).then((res) => {
        this.editForm = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          updateFault({
            id: this.editForm.id,
            status: 1,
            feedback: this.editForm.feedback,
          }).then((res) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(`确认删除已选中的${row.id ? 1 : ids.length}故障申报数据吗？`)
        .then(() => {
          delFault(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
